package cn.craftyun.yunexam.service.impl;

import cn.craftyun.yunexam.entity.User;
import cn.craftyun.yunexam.mapper.UserMapper;
import cn.craftyun.yunexam.service.UserService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private HttpServletRequest request;

    @Override
    public User findByStuNumber(String stuNumber) {
        // 用户登录
        Wrapper<User> wrapper = new QueryWrapper<User>()
                .eq("stu_number", stuNumber)
                .last("limit 1");
        return getBaseMapper().selectOne(wrapper);
    }

    @Override
    public User getLoginUser() {
        return (User) request.getSession().getAttribute("examUser");
    }

    @Override
    public User getUserByName(String name) {
        Wrapper<User> wrapper = new QueryWrapper<User>().eq("name", name).last("limit 1");
        return getBaseMapper().selectOne(wrapper);
    }
}
